﻿/**
 * ColorMunch 1.0: AS3 library for the Adobe Kuler API (http://code.google.com/p/colormunch/)
 *
 * ColorMunch is (c) 20009 Ben Kanizay
 * This software is released under the MIT License (http://www.opensource.org/licenses/mit-license.php)
 *
 */

/**
 * @author Ben Kanizay (http://beekay.biz)
 */

package beekay.colormunch {

	import flash.display.Sprite;
	
	/**
	 * Holds Kuler theme swatch data
	 * 
	 */
	public class Swatch {

		private var hexString:String;
		private var hex:uint;
		private var colourMode:String;
		private var channel1:Number;
		private var channel2:Number;
		private var channel3:Number;
		private var channel4:Number;
		private var allChannels:Object = new Object;
		private var swatchIndex:int;
		private var swatchSprite:Sprite = null;
		private var isDark:Boolean;


		/**
		 * Creates a new Swatch
		 * @param hexColour  The hex colour value
		 * @param colourMode  The colour mode
		 * @param channel1  The Channel 1 value
		 * @param channel2  The Channel 2 value
		 * @param channel3  The Channel 3 value
		 * @param channel4  The Channel 4 value
		 * @param swatchIndex  The swatch index
		 * @constructor
		 */
		public function Swatch(hexColour:String, colourMode:String, channel1:String, channel2:String, channel3:String, channel4:String, swatchIndex:String) {

			// set the values
			this.hexString = (hexColour.substr(0,2) == "0x") ? hexColour : "0x" + hexColour;
			this.hex = parseInt(this.hexString);
			this.colourMode = colourMode;
			this.channel1 = Number(channel1);
			this.channel2 = Number(channel2);
			this.channel3 = Number(channel3);
			this.channel4 = Number(channel4);
			this.swatchIndex = parseInt(swatchIndex);
			this.allChannels = {
				channel1 : this.channel1, 
				channel2 : this.channel2, 
				channel3 : this.channel3, 
				channel4 : this.channel4
			};
			
			// check whether it's a dark colour or not
			var r:Number = this.hex >> 16 & 255;
			var g:Number = this.hex >> 8 & 255;
			var b:Number = this.hex & 255;
			var brightness:Number = Math.max(r, g, b); 
			this.isDark = (brightness < 192) ? true : false;
		}




		/**
		 * Get the hex value of this swatch
		 * 
		 * @return Hex value
		 * 
		 */
		public function getHex() : uint {
			return this.hex;
		}

		/**
		 * Get the hex value of this swatch as a String
		 * 
		 * @return Hex value
		 * 
		 */
		public function getHexSring() : String {
			return this.hexString;
		}
		
		/**
		 * Get the colour mode of this swatch
		 * 
		 * @return Colour mode
		 * 
		 */
		public function getColourMode() : String {
			return this.colourMode;
		}
		
		/**
		 * Get the Channel 1 value
		 * 
		 * @return Channel 1
		 * 
		 */
		public function getChannel1() : Number {
			return this.channel1;
		}
		
		/**
		 * Get the Channel 2 value
		 * 
		 * @return Channel 2
		 * 
		 */
		public function getChannel2() : Number {
			return this.channel2;
		}
		
		/**
		 * Get the Channel 3 value
		 * 
		 * @return Channel 3
		 * 
		 */
		public function getChannel3() : Number {
			return this.channel3;
		}
		
		/**
		 * Get the Channel 4 value
		 * 
		 * @return Channel 4
		 * 
		 */
		public function getChannel4() : Number {
			return this.channel4;
		}
		
		/**
		 * Get all the channels in one object
		 * 
		 * @return All channels
		 * 
		 */
		public function getAllChannels() : Object {
			return this.allChannels;
		}

		/**
		 * Get the swatch index (in relation to the other swatches in the same theme)
		 * 
		 * @return This swatch's index
		 * 
		 */
		public function getSwatchIndex() : int {
			return this.swatchIndex;
		}
		
		/**
		 * Get a square shape (20x20) the colour of this swatch
		 * 
		 * @return Swatch coloured square
		 * 
		 */
		public function getSwatchSprite() : Sprite {
			if (this.swatchSprite == null) {
				this.swatchSprite = new Sprite();
				this.swatchSprite.graphics.beginFill(this.hex, 1);
				this.swatchSprite.graphics.drawRect(0, 0, 20, 20);
				this.swatchSprite.graphics.endFill();
			}
			return this.swatchSprite;
		}

		/**
		 * Check if this swatch is a dark colour
		 * <p>Handy if you're placing text over this colour and want to know whether to use a light or dark colour for the text</p>
		 * 
		 * @return Whether this colour is dark <code>true</code> or light <code>false</code>
		 * 
		 */
		public function isDarkColour() : Boolean {
			return this.isDark;
		}

	}
}
